package com.top.horizon.th1804app.base;

import android.app.ProgressDialog;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;

import com.top.horizon.th1804app.utils.ToastUitl;
import com.top.horizon.th1804app.view.CustomDialog;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseActivity extends AppCompatActivity implements CustomDialog.OnClickChildVIewListener {
    private Bundle mSavedInstanceState;
    private ProgressDialog progressDialog;
    private List<String> mPermissionList = new ArrayList<>();
    protected CustomDialog customDialog;
    private Handler mHandler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(Message msg) {
            mHandler.removeMessages(0);
            hideProressDialog();
            showToast("请求超时");
        }
    };
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutResource());
        customDialog = new CustomDialog(this, 0);
        progressDialog = new ProgressDialog(this);
        progressDialog.setCancelable(false);
        this.mSavedInstanceState = savedInstanceState;
        initView();
        initData();
    }

    /**
     * 显示选择对话框
     *
     * @param layoutId
     */
    public void showChooseDialog(int layoutId) {
//        if (customDialog == null)
        customDialog.setLayout(layoutId);
        customDialog.setOnClickChildVIewListener(this);
        customDialog.showDialog();
    }

    public void dismissDialog() {
        if (customDialog != null)
            customDialog.dismiss();
    }

    /**
     * 显示进度条
     *
     * @param content
     */
    public void showProgressDialog(String content) {
        if (progressDialog != null) {
            if (TextUtils.isEmpty(content))
                progressDialog.setMessage("加载中...");
            else
                progressDialog.setMessage(content);
            progressDialog.show();
        }
        mHandler.sendEmptyMessageDelayed(0,10_000);
    }

    /**
     * 添加需要请求的权限
     *
     * @param permissionName
     */
    public void addPermission(String permissionName) {
        mPermissionList.add(permissionName);
    }

    /**
     * 找到请求的权限
     *
     * @param permissions
     * @return
     */
    private List<String> findApplyForPermission(List<String> permissions) {
        List<String> needRequestPermissions = new ArrayList<>();
        for (String perm : permissions) {
            if (ContextCompat.checkSelfPermission(this, perm) != PackageManager.PERMISSION_GRANTED) {
                needRequestPermissions.add(perm);
            }
        }
        return needRequestPermissions;
    }

    /**
     * 申请注册权限
     */
    public void requestPermission() {
        if (Build.VERSION.SDK_INT > 23) {
            List<String> needRequestPermission = findApplyForPermission(mPermissionList);
            if (needRequestPermission != null && needRequestPermission.size() > 0){
                ActivityCompat.requestPermissions(this,needRequestPermission.toArray(new String[needRequestPermission.size()]),1);
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode!=1)return;

    }

    /**
     * 隐藏进度条
     */
    public void hideProressDialog() {
        mHandler.removeMessages(0);
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
    }

    /**
     * 显示短时间吐司
     *
     * @param message
     */
    public void showToast(String message) {
        ToastUitl.showToast(this, message);
    }

    /**
     * 显示长时间吐司
     *
     * @param message
     */
    public void showLongToast(String message) {
        ToastUitl.showLongToast(this, message);
    }

    /**
     * 弹出短时间中间显示吐司
     *
     * @param message
     */
    public void showShortCenterToast(String message) {
        ToastUitl.showShortCenterToast(this, message);
    }

    protected abstract int getLayoutResource();
    protected abstract void initView();
    protected abstract void initData();

    @Override
    public  void onClickChildView(View view, int mLayoutId) {

    }
}
